package com.dataseek.iot.sys.manager.handler;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dataseek.iot.core.constant.ResponseConst;
import com.dataseek.iot.core.response.ResponseEntity;
import com.dataseek.iot.core.handler.ServiceHandler;
import com.dataseek.iot.mysql.entity.User;
import com.dataseek.iot.mysql.service.impl.UserServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Component;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * @author cc
 * @Title: 用户信息服务 handler
 * @Description: 用户信息服务 handler
 * @date 2021/9/54:37 下午
 * @since 1.0
 */
@Component
public class UserServiceHandler extends ServiceHandler<User> {
    @Autowired
    UserServiceImpl userService;

    @Autowired
    PasswordEncoder passwordEncoder;

    @Override
    public Mono<ResponseEntity> saveOne(Object entity) {
        ResponseEntity responseEntity = new ResponseEntity();
        User user = (User)entity;
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("username", user.getUsername());
        if(isExist(wrapper)){
            responseEntity.setCode(ResponseConst.DUPLICATION_CODE);
            responseEntity.setMsg("用户名已经存在");
            responseEntity.setData(user.getUsername());
            return Mono.just(responseEntity);
        }
        wrapper.or(true).eq("nickname", user.getNickname());
        if(isExist(wrapper)){
            responseEntity.setCode(ResponseConst.DUPLICATION_CODE);
            responseEntity.setMsg("昵称已经存在");
            responseEntity.setData(user.getNickname());
            return Mono.just(responseEntity);
        }
        wrapper.or(true).eq("mail", user.getMail());
        if(isExist(wrapper)){
            responseEntity.setCode(ResponseConst.DUPLICATION_CODE);
            responseEntity.setMsg("邮箱已经存在");
            responseEntity.setData(user.getMail());
            return Mono.just(responseEntity);
        }
        wrapper.or(true).eq("tel", user.getTel());
        if(isExist(wrapper)){
            responseEntity.setCode(ResponseConst.DUPLICATION_CODE);
            responseEntity.setMsg("手机号已经存在");
            responseEntity.setData(user.getTel());
            return Mono.just(responseEntity);
        }

        Boolean b = userService.save((User) entity);
        return this.createResponse(b);
    }

    @Override
    public Mono<ResponseEntity> saveBatch(List lists) {
//        List<User> users = new ArrayList<>();
//        for(Object o : lists){
//            User u = (User)o;
//            if(Objects.nonNull( u.getPassword())){
//                String password = u.getPassword();
//                password = passwordEncoder.encode(password);
//                u.setPassword(password);
//                users.add(u);
//            }
//        }
        Boolean b = userService.saveBatch(lists);
        return this.createResponse(b);
    }


    @Override
    public Mono<ResponseEntity> removeById(String id) {
        Boolean b = userService.removeById(id);
        return this.createResponse(b);
    }

    @Override
    public Mono<ResponseEntity> removeAll(List ids) {
        Boolean b = userService.removeByIds(ids);
        return this.createResponse(b);
    }

    @Override
    public Mono<ResponseEntity> updateOne(Object entity) {
        User u = (User)entity;
        if(Objects.nonNull( u.getPassword())){
            String password = u.getPassword();
            password = passwordEncoder.encode(password);
            u.setPassword(password);
        }
        Boolean b = userService.updateById((User) entity);
        return this.createResponse(b);
    }

    @Override
    public Mono<ResponseEntity> updateBatch(List entities) {
//        List<User> users = new ArrayList<>();
//        for(Object o : entities){
//            User u = (User)o;
//            if(Objects.nonNull( u.getPassword())){
//                String password = u.getPassword();
//                password = passwordEncoder.encode(password);
//                u.setPassword(password);
//                users.add(u);
//            }
//
//        }
        Boolean b =  userService.updateBatchById(entities);
        return this.createResponse(b);
    }

    @Override
    public Mono<ResponseEntity> findOne(String id) {
        User user = userService.getById(id);
        return this.createResponse(user);
    }

    @Override
    public Mono<ResponseEntity> page(Page p) {
        Page userPage = userService.page(p);
        return this.createResponse(userPage);
    }

    @Override
    public Mono<ResponseEntity> page(Page p, Wrapper wrapper) {
        Page userPage = userService.page(p, wrapper);
        return this.createResponse(userPage);
    }

    @Override
    public Flux<ResponseEntity> find(List ids) {
        List<User> lists = userService.listByIds(ids);
        return this.createResponse(lists);
    }

    @Override
    public Flux<ResponseEntity> find(Wrapper wrapper) {
        List<User> lists = userService.list(wrapper);
        return this.createResponse(lists);
    }

    @Override
    public Flux findAll() {
        List<User> lists = userService.list();
        return this.createResponse(lists);
    }

    @Override
    public Mono<ResponseEntity> remove(Wrapper wrapper) {
        Boolean b = userService.remove(wrapper);
        return this.createResponse(b);
    }

    @Override
    public Mono<ResponseEntity> remove(String id) {
        Boolean b = userService.removeById(id);
        return this.createResponse(b);
    }

    @Override
    public Mono<ResponseEntity> update(Wrapper wrapper) {
        Boolean b = userService.update(wrapper);
        return this.createResponse(b);
    }

    @Override
    public Mono<ResponseEntity> defaultResponseOne(ResponseEntity responseEntity) {
        return Mono.just(responseEntity);
    }

    @Override
    public Flux<ResponseEntity> defaultResponseTwo(ResponseEntity responseEntity) {
        return Flux.just(responseEntity);
    }


    /**
     * 验证是否存在
     * @param wrapper
     * @return
     */
    public Boolean isExist(QueryWrapper wrapper){
        if(Objects.nonNull(userService.getOne(wrapper))) return true;
        return false;
    }
}
